package com.jjwxc.jwjskandriod.readActivity.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ListWordResponse;
import com.jjwxc.jwjskandriod.readActivity.local.ReadSettingManager;
import com.jjwxc.jwjskandriod.readActivity.utils.Constant;
import com.jjwxc.jwjskandriod.readActivity.utils.FileUtil;
import com.jjwxc.jwjskandriod.readActivity.utils.FileUtils;
import com.jjwxc.jwjskandriod.readActivity.view.PageLoader;

import java.util.ArrayList;
import java.util.List;

public class TypefaceDialog extends Dialog {
    private PageLoader mPageLoader;
    private Activity mActivity;

    private List<ListWordResponse.DataBean> data = new ArrayList<>();
    private ReadSettingManager mSettingManager;
    private String fileName;

    private List<ConstraintLayout> mConstList = new ArrayList<>();
    private List<ImageView> mImageList = new ArrayList<>();
    private List<ImageView> mImageSList = new ArrayList<>();
    private List<TextView> mTvList = new ArrayList<>();
    private PopTypefaceListener typefaceListener;

    public TypefaceDialog(@NonNull Activity activity, PageLoader mPageLoader,PopTypefaceListener typefaceListener) {
        super(activity);
        mActivity = activity;
        this.mPageLoader = mPageLoader;
        this.typefaceListener = typefaceListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_typeface);
        setUpWindow();
        initData();
    }


    //设置Dialog显示的位置
    private void setUpWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        window.getDecorView().setBackground(null);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    List<ConstraintLayout> lcList = new ArrayList<>();
    List<ImageView> imgOneList = new ArrayList<>();
    List<ImageView> imgTwoList = new ArrayList<>();
    List<TextView> tvList = new ArrayList<>();
    int[] imgFalse = {R.mipmap.xitong_false, R.mipmap.siyuan_false, R.mipmap.mengwa_false, R.mipmap.heiti_false, R.mipmap.maoti_false, R.mipmap.kaiti_false};
    int[] imgTrue = {R.mipmap.xitong_true, R.mipmap.siyuan_true, R.mipmap.mengwa_true, R.mipmap.heiti_true, R.mipmap.maoti_true, R.mipmap.kaiti_true};

    private void initData() {
        mSettingManager = ReadSettingManager.getInstance();

        ConstraintLayout ll_fanhui = findViewById(R.id.ll_fanhui);

        ConstraintLayout cl_one = findViewById(R.id.cl_one);
        ImageView img_one = findViewById(R.id.img_one);
        ImageView img_s_one = findViewById(R.id.img_s_one);
        TextView tv_one = findViewById(R.id.tv_one);
        lcList.add(cl_one);
        imgOneList.add(img_one);
        imgTwoList.add(img_s_one);
        tvList.add(tv_one);

        ConstraintLayout cl_two = findViewById(R.id.cl_two);
        ImageView img_two = findViewById(R.id.img_two);
        ImageView img_s_two = findViewById(R.id.img_s_two);
        TextView tv_two = findViewById(R.id.tv_two);
        lcList.add(cl_two);
        imgOneList.add(img_two);
        imgTwoList.add(img_s_two);
        tvList.add(tv_two);

        ConstraintLayout cl_three = findViewById(R.id.cl_three);
        ImageView img_three = findViewById(R.id.img_three);
        ImageView img_s_three = findViewById(R.id.img_s_three);
        TextView tv_three = findViewById(R.id.tv_three);
        lcList.add(cl_three);
        imgOneList.add(img_three);
        imgTwoList.add(img_s_three);
        tvList.add(tv_three);

        ConstraintLayout cl_four = findViewById(R.id.cl_four);
        ImageView img_four = findViewById(R.id.img_four);
        ImageView img_s_four = findViewById(R.id.img_s_four);
        TextView tv_four = findViewById(R.id.tv_four);
        lcList.add(cl_four);
        imgOneList.add(img_four);
        imgTwoList.add(img_s_four);
        tvList.add(tv_four);

        ConstraintLayout cl_five = findViewById(R.id.cl_five);
        ImageView img_five = findViewById(R.id.img_five);
        ImageView img_s_five = findViewById(R.id.img_s_five);
        TextView tv_five = findViewById(R.id.tv_five);
        lcList.add(cl_five);
        imgOneList.add(img_five);
        imgTwoList.add(img_s_five);
        tvList.add(tv_five);

        ConstraintLayout cl_six = findViewById(R.id.cl_six);
        ImageView img_six = findViewById(R.id.img_six);
        ImageView img_s_six = findViewById(R.id.img_s_six);
        TextView tv_six = findViewById(R.id.tv_six);
        lcList.add(cl_six);
        imgOneList.add(img_six);
        imgTwoList.add(img_s_six);
        tvList.add(tv_six);
        new FFNetWork().post(Url.listWordFile, null, new FFNetWorkCallBack<ListWordResponse>() {
            @Override
            public void onSuccess(ListWordResponse response) {
                if (response.getCode() == 200) {
                    data.clear();
                    ListWordResponse.DataBean bean = new ListWordResponse.DataBean();
                    bean.setUrl("");
                    data.add(bean);
                    data.addAll(response.getData());
                    setTypeface();
                }
            }

            @Override
            public boolean onFail(ListWordResponse response) {
                return false;
            }
        }, ListWordResponse.class);

        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (isDownFile) {
                    Log.e("下载未完成", "关闭了窗口，进入后台下载");
                }
            }
        });
    }

    public void setTypeface() {
        int pos = mSettingManager.getTypefacePos();
        String path = mSettingManager.getTypefacePath();
        for (int s = 0; s < lcList.size(); s++) {
            if (s != 0) {
                String[] strings = data.get(s).getUrl().split("/");
                fileName = strings[strings.length - 1];
                if (FileUtil.checkFileExist(Constant.BOOK_CACHE_PATH + fileName)) {
                    tvList.get(s).setText("已下载");
                }
            }

            if (s == pos) {
                if (!StUtils.isEmpty(path) && FileUtil.checkFileExist(path)) {
                    lcList.get(s).setBackgroundResource(R.drawable.typeface_select_true);
                    imgOneList.get(s).setImageResource(imgTrue[s]);
                    imgTwoList.get(s).setVisibility(View.VISIBLE);
                    tvList.get(s).setVisibility(View.GONE);
                }else{
                    //重置系统字体
                    mSettingManager.setTypefacePath(0, "");
                    lcList.get(0).setBackgroundResource(R.drawable.typeface_select_true);
                    imgOneList.get(0).setImageResource(imgTrue[0]);
                    imgTwoList.get(0).setVisibility(View.VISIBLE);
                    tvList.get(0).setVisibility(View.GONE);
                }
            } else {
                lcList.get(s).setBackgroundResource(R.drawable.typeface_select_false);
                imgOneList.get(s).setImageResource(imgFalse[s]);
                imgTwoList.get(s).setVisibility(View.GONE);
                tvList.get(s).setVisibility(View.VISIBLE);
            }
        }

        for (int i = 0; i < lcList.size(); i++) {
            int finalI = i;
            lcList.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String[] strings = data.get(finalI).getUrl().split("/");
                    String fileName = strings[strings.length - 1];
                    if (isDownFile) {
                        FFApplication.showToast("下载字体中请稍等");
                        return;
                    }
                    for (int s = 0; s < lcList.size(); s++) {
                        if (s == finalI) {
                            //系统字体
                            if (finalI == 0) {
                                mSettingManager.setTypefacePath(0, "");
                                //重新设置字体字号
                                mPageLoader.setTextSize(mSettingManager.getTextSize(), mSettingManager.getTextInterval(), "");
                                lcList.get(s).setBackgroundResource(R.drawable.typeface_select_true);
                                imgOneList.get(s).setImageResource(imgTrue[s]);
                                imgTwoList.get(s).setVisibility(View.VISIBLE);
                                tvList.get(s).setVisibility(View.GONE);
                                //回调
                                typefaceListener.onValueClick();
                            } else {
                                if (FileUtil.checkFileExist(Constant.BOOK_CACHE_PATH + fileName)) {
                                    mSettingManager.setTypefacePath(finalI, fileName);
                                    //重新设置字体字号
                                    mPageLoader.setTextSize(mSettingManager.getTextSize(), mSettingManager.getTextInterval(), Constant.BOOK_CACHE_PATH + fileName);
                                    lcList.get(s).setBackgroundResource(R.drawable.typeface_select_true);
                                    imgOneList.get(s).setImageResource(imgTrue[s]);
                                    imgTwoList.get(s).setVisibility(View.VISIBLE);
                                    tvList.get(s).setVisibility(View.GONE);
                                    //回调
                                    typefaceListener.onValueClick();
                                } else {
                                    FFApplication.showToast("开始下载字体");
                                    downFile(fileName, data.get(finalI).getUrl(), tvList.get(finalI));
                                }
                            }

                        } else {
                            if (FileUtil.checkFileExist(Constant.BOOK_CACHE_PATH + fileName)) {
                                lcList.get(s).setBackgroundResource(R.drawable.typeface_select_false);
                                imgOneList.get(s).setImageResource(imgFalse[s]);
                                imgTwoList.get(s).setVisibility(View.GONE);
                                tvList.get(s).setVisibility(View.VISIBLE);
                            }
                        }
                    }


                }
            });
        }
    }

    boolean isDownFile = false;

    public void downFile(String fileName, String url, TextView textView) {
        isDownFile = true;
        new FFNetWork().downFile(url, Constant.BOOK_CACHE_PATH + fileName, new FFNetWork.OnDownloadListener() {
            @Override
            public void onDownloadSuccess() {
                FFApplication.showToast("下载字体完成");
                isDownFile = false;
                FFApplication.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("已下载");
                    }
                });

            }

            @Override
            public void onDownloading(int progress) {
                FFApplication.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("下载中" + progress + "%");
                    }
                });

            }

            @Override
            public void onDownloading(long complete, long progress, long all) {

            }

            @Override
            public void onDownloadFailed(String err) {
                isDownFile = false;
                //下载失败删除
                FileUtils.fileDelete(Constant.BOOK_CACHE_PATH + fileName);
                Log.e("---", err);
            }
        });
    }
    public interface PopTypefaceListener {
        void onValueClick();

    }
}
